//
//  BeegroupMoreController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/16.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "BeegroupMoreController.h"
#import "BeeGroupListObj.h"
#import "InviteRecordController.h"
#import "BeeMemberController.h"

#import "BeeGroupMoreObj.h"

@interface BeegroupMoreController ()<UITableViewDelegate,UITableViewDataSource>
{
    BeeGroupListObj *beeObj;
    UITableView *mainView;
    UIButton *logoutBtn;
    
    NSMutableArray *titleList;
}
@end

@implementation BeegroupMoreController

-(instancetype)initWithBeeObj:(BeeGroupListObj *)obj
{
    self = [super init];
    
    if (self) {
        beeObj = obj;
    }
    
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self setTitleList];
    [mainView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    titleList = [[NSMutableArray alloc] init];
    
    [self.largeTitle setTitle:beeObj.groupName];
    [self buildView];
    
}

-(void)setTitleList
{
    [titleList removeAllObjects];
    
    //邀请好友
    {
        BeeGroupMoreObj *obj = [BeeGroupMoreObj new];
        obj.title = NSLocalizedString(@"bGroupInvite", nil);
        obj.type = MoreInviteFriend;
        [titleList addObject:obj];
    }
    
    //输入邀请码
    if (!beeObj.isAdmin && [NSString isEmptyString:beeObj.usedInvitation]) {
        BeeGroupMoreObj *obj = [BeeGroupMoreObj new];
        obj.title = NSLocalizedString(@"bGroupInviteCode", nil);
        obj.type = MoreInviteCode;
        [titleList addObject:obj];
    }

    //邀请记录
    {
        BeeGroupMoreObj *obj = [BeeGroupMoreObj new];
        obj.title = NSLocalizedString(@"bGroupInviteList", nil);
        obj.type = MoreInviteRecord;
        [titleList addObject:obj];
    }
    
    //管理员
    if (beeObj.isAdmin) {
        BeeGroupMoreObj *obj = [BeeGroupMoreObj new];
        obj.title = NSLocalizedString(@"bGroupManager", nil);
        obj.type = MoreMember;
        [titleList addObject:obj];
    }
}

-(void)buildView
{
    mainView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.largeTitle.height, IPHONEWIDTH, IPHONEHEIGHT-self.largeTitle.height) style:UITableViewStylePlain];
    mainView.separatorStyle = UITableViewCellSeparatorStyleNone;
    mainView.alwaysBounceVertical = NO;
    mainView.backgroundColor = [UIColor clearColor];
    mainView.delegate = self;
    mainView.dataSource = self;
    [self.view addSubview:mainView];
    
    logoutBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.addBackGoundColor(UIColorFromRGB(0xF0F0F0));
        make.addTitleColor(FBEE_COLOR_THEME,UIControlStateNormal);
        make.addTitleColor(FBEE_COLOR_INFO,UIControlStateHighlighted);
        make.addTarget(self,@selector(logoutAction));
        make.layer.cornerRadius = 6;
        make.addFont([UIFont systemFontOfSize:15.0f]);
        make.addTitle(NSLocalizedString(@"bGroupQuit", nil));
        make.setFrame(CGRectMake(15, 0, IPHONEWIDTH - 30, 50));
        make.bottom = IPHONEHEIGHT - TabbarHeight - 45;
        [self.view addSubview:make];
    }];
    
}

#pragma mark - TableView Cell

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titleList.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"CELLID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    BeeGroupMoreObj *obj = [titleList objectAtIndex:indexPath.row];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *title = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.addTextColor(FBEE_COLOR_TEXT).addFont([UIFont systemFontOfSize:18.0f]);
            make.setFrame(CGRectMake(20, (70 - 20)/2, IPHONEWIDTH - 80, 20));
            make.tag = 111;
        }];
        [cell addSubview:title];
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 9, 16)];
        arrow.image = [UIImage imageNamed:@"cell_arrow_r"];
        arrow.right = IPHONEWIDTH - 15;
        arrow.top = (70 - 16)/2;
        [cell addSubview:arrow];
        
        UILabel *info = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.addTextColor(FBEE_COLOR_TEXT).addFont([UIFont systemFontOfSize:18.0f]).addTextAlignment(NSTextAlignmentRight);
            make.setFrame(CGRectMake(0, (70 - 20)/2, 80, 20));
            make.tag = 222;
            make.right = arrow.left - 10;
        }];
        [cell addSubview:info];
    }
    
    UILabel *title = [cell viewWithTag:111];
    UILabel *info = [cell viewWithTag:222];
    title.text = info.text = @"";
    
    title.text = obj.title;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (![UserInfoManager sharedManager].isLogin) {
        [[JumpManager sharedManager] jumpToLogin];
        return;
    }
    
    BeeGroupMoreObj *obj = [titleList objectAtIndex:indexPath.row];
    
    if(obj.type == MoreInviteFriend)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Share object:nil];
        return;
    }
    
    if(obj.type == MoreInviteCode)
    {
        [[JumpManager sharedManager] jumpToBeeInviteCode:beeObj];
        return;
    }
    
    if(obj.type == MoreInviteRecord)
    {
        InviteRecordController *vc =[[InviteRecordController alloc] initWithBeeObj:beeObj];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    if(obj.type == MoreMember)
    {
        BeeMemberController *vc =[[BeeMemberController alloc] initWithBeeObj:beeObj];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    
    

    
}

#pragma maerk - logout

-(void)logoutAction
{
    if ([LoadingHelper isLoadingFromView:self.view]) {
        return;
    }
    
    [self post:BeeGroupQuit_URL param:@{@"gid":beeObj.groupId,@"uid":[UserInfoManager sharedManager].userId} code:BeeGroupQuit_URL_Code];
    
    [LoadingHelper showLoadingOnView:self.view];
}

#pragma mark - response

-(void)requestSuccess:(id)data code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
    
    NSString *info = SafeGetStringValue([data objectForKey:@"msg"]);
    [NoticePopHelper showNoticeText:info];
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_QuitBeeGroupSuccess object:nil];
    [self navback];
}

-(void)requestLogicError:(id)data code:(NSInteger)code
{
    [super requestLogicError:data code:code];
    
    [LoadingHelper removeLoadingOnView:self.view];
}

-(void)requestFailure:(NSError *)error code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
}

@end
